/*
 * @(#)MailMessage.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.mail.core;

import java.util.ArrayList;
import java.util.List;

import javax.mail.Message;
import javax.mail.internet.InternetAddress;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MailMessage extends com.dcivision.framework.bean.AbstractBaseObject {

  public static final String REVISION = "$Revision: 1.7.42.2 $";

  private static final Log log = LogFactory.getLog(MailMessage.class);
  
  public final static int PRIORITY_HIGHEST = 5;
  public final static int PRIORITY_HIGH = 4;
  public final static int PRIORITY_NORMAL = 3;
  public final static int PRIORITY_LOW = 2;
  public final static int PRIORITY_LOWEST = 1;

  private Message message;
  private List attachmentList;
  private boolean isTextOnly;
  private boolean hasAttachment;
  private int priority;
  private String[] flags;

  public MailMessage(Message message) {
    this.message = message;
    this.id = new Integer(this.message.getMessageNumber());

    try {
      this.isTextOnly = message.isMimeType("text/plain");
      this.hasAttachment = message.isMimeType("multipart/*");
      String pri[] = this.message.getHeader("X-Priority");
      if (pri == null) {
        this.priority = PRIORITY_NORMAL;
      } else {
        if (pri[0].indexOf("1") >= 0) {
          this.priority = PRIORITY_HIGHEST;
        } else if (pri[0].indexOf("2") >= 0) {
          this.priority = PRIORITY_HIGH;
        } else if (pri[0].indexOf("3") >= 0) {
          this.priority = PRIORITY_NORMAL;
        } else if (pri[0].indexOf("4") >= 0) {
          this.priority = PRIORITY_LOW;
        } else if (pri[0].indexOf("5") >= 0) {
          this.priority = PRIORITY_LOWEST;
        } else {
          this.priority = PRIORITY_NORMAL;
          for (int i = 0; i < pri.length; i++ ) {
            log.debug("UNKNOWN Priority: " + pri[i]);
          }
        }
      }
    } catch (Exception e) {
      log.error(e, e);
    }

    attachmentList = new ArrayList();
  }

  public Message getSourceMessage() {
    return message;
  }

  public InternetAddress[] getFrom() throws MailRetrieverException {
    InternetAddress[] ias = new InternetAddress[0];
    try {
      ias = (InternetAddress[]) message.getFrom();
    } catch (Exception e) {
      throw new MailRetrieverException();
    }
    return ias;
  }

  public InternetAddress[] getRecipients(Message.RecipientType type) throws
      MailRetrieverException {
    InternetAddress[] ias = new InternetAddress[0];
    try {
      ias = (InternetAddress[]) message.getRecipients(type);
    } catch (Exception e) {
      throw new MailRetrieverException();
    }
    return ias;
  }

  public void addMailAttachment(MailAttachment attachment) {
    attachmentList.add(attachment);
  }

  public void setMailAttachmentList(List attachmentList) {
    this.attachmentList = attachmentList;
  }

  public MailAttachment[] getMailAttachments() {
    MailAttachment[] atts = new MailAttachment[attachmentList.size()];
    System.arraycopy(attachmentList.toArray(), 0, atts, 0, attachmentList.size());
    return atts;
  }

  public boolean isTextOnly() {
    return this.isTextOnly;
  }

  public boolean hasAttachment() {
    return this.hasAttachment;
  }

  public String[] getFlags() {
    return this.flags;
  }

  public int getPriority() {
    return this.priority;
  }
}
